<!DOCTYPE html>
<!-- ng-app -->
<html lang="en" ng-app="app">
<head>
    <meta charset="UTF-8">
    <title></title>
    <script src="../../../js/plugins/angular/angular.min.js" type="text/javascript"></script>
</head>
<body>
    <div ng-controller="cont">
        <input type="text" ng-model="cname" size="6">
        <button ng-click="cset()">设置</button>
        <button ng-click="cshow()">显示</button>
        <button ng-click="cdel()">删除</button>
        <div class="tip">缓存的值是：{{cvalue}}</div>
    </div>

    <script>
        angular.module('app',[])
            .service("cache",function ($cacheFactory) {
                /* 创建一个名为cache的服务，该服务返回一个名为test的缓存对象，控制器中的注入 */
                return $cacheFactory("test");
            })
            .controller("cont",function ($scope,cache) {
                /**
                 * 设置缓存
                 */
                $scope.cset = function () {
                    cache.put('mytest',$scope.cname);
                }
                /**
                 * 显示缓存
                 */
                $scope.cshow = function () {
                    var tcache = cache.get('mytest');
                    $scope.cvalue = tcache ? tcache : "空值";
                }
                /**
                 * 删除缓存
                 */
                $scope.cdel = function () {
                    cache.remove("mytest");
                }
            })


    </script>
</body>
</html>